package com.example.xcService.pojo.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.AgentOrder;
import com.example.entity.OrderPolling;
import com.example.entity.OrderRefund;
import com.example.pojo.enums.OrderEnum;
import com.example.pojo.enums.PollingEnum;
import com.example.pojo.enums.ProductTypeENUM;
import com.example.pojo.rabbitmq.NoticeFanoutProduce;
import com.example.pojo.rabbitmq.OrderFanoutProduce;
import com.example.pojo.rabbitmq.QueryFanoutProduce;
import com.example.pojo.rabbitmq.RefundFanoutProduce;
import com.example.service.*;
import com.example.xcService.pojo.tool.OrderTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling
@Slf4j
public class ScheduleTask {
    @Autowired
    IAgentOrderService agentOrderService;

    @Autowired
    OrderTool orderTool;

    @Autowired
    IOrderPollingService pollingService;

    @Autowired
    IOrderRefundService refundService;

    @Autowired
    IRefundOrderService refundOrderService;

    @Autowired
    IRefundAutomoticService automoticService;

    @Autowired
    NoticeFanoutProduce orderFanoutProduce;

    @Autowired
    OrderFanoutProduce submitProduce;

    @Autowired
    QueryFanoutProduce queryFanoutProduce;

    @Autowired
    RefundFanoutProduce refundFanoutProduce;



    //订单失败退款
    @Scheduled(fixedDelay = 30*1000)
    public  void  Update(){
        LambdaQueryWrapper<AgentOrder> wrapper=new LambdaQueryWrapper<AgentOrder>();
        wrapper.and(wrappers->wrappers.eq(AgentOrder::getOrderStatus, OrderEnum.REFUNDERROR));
        wrapper.eq(AgentOrder::getProductType, ProductTypeENUM.Equity);

        Page page=new Page();
        page.setSize(1500);
        page.setCurrent(1);
        IPage result=agentOrderService.page(page,wrapper);
        List<AgentOrder> list=result.getRecords();

        for(AgentOrder order : list){
            refundFanoutProduce.OrderRefund(order.getId());
        }
    }

    //查询订单
//    @Scheduled(cron = "0 */1 * * * ?")
    @Scheduled(fixedDelay = 30*1000)
    public  void  Query(){

        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time=dtf.format(LocalDateTime.now());
        LambdaQueryWrapper<OrderPolling> wrapper=new LambdaQueryWrapper<OrderPolling>();
        wrapper.and(wrappers->wrappers.eq(OrderPolling::getOrderStatus, PollingEnum.UNDERWAY).or().eq(OrderPolling::getOrderStatus,PollingEnum.WRONG).or().eq(OrderPolling::getOrderStatus,PollingEnum.ERROR));

        Page page=new Page();
        page.setSize(1500);
        page.setCurrent(1);
        IPage result=pollingService.page(page,wrapper);
        List<OrderPolling> list=result.getRecords();
        for(OrderPolling polling : list){
            queryFanoutProduce.OrderQuery(polling.getPollingId());
        }
    }

//    //轮询订单
//    @Scheduled(fixedRate = 30*1000)
//    public  void  WAITOrder(){
//        LambdaQueryWrapper<AgentOrder> wrapper=new LambdaQueryWrapper<AgentOrder>();
//        wrapper.and(wrappers->wrappers.eq(AgentOrder::getOrderStatus,OrderEnum.ERROR));
//        wrapper.eq(AgentOrder::getProductType, ProductTypeENUM.Equity);
//        wrapper.ne(AgentOrder::getAgentId,148);
//
//        Page page=new Page();
//        page.setSize(1500);
//        page.setCurrent(1);
//        IPage result=agentOrderService.page(page,wrapper);
//        List<AgentOrder> list=result.getRecords();
//        // 创建一个固定大小的线程池:
//        ExecutorService es = new ThreadPoolExecutor(500, 1500,
//                60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
//        for(AgentOrder order : list){
//            es.submit(new SubmitTask(order,orderTool));
//        }
//        es.shutdown();
//    }


//    //提交订单
    @Scheduled(fixedDelay = 30*1000)
    public  void  Submit(){
        LambdaQueryWrapper<AgentOrder> wrapper=new LambdaQueryWrapper<AgentOrder>();
        wrapper.and(wrappers->wrappers.eq(AgentOrder::getOrderStatus,OrderEnum.ERROR));
        wrapper.eq(AgentOrder::getProductType, ProductTypeENUM.Equity);

        Page page=new Page();
        page.setSize(1500);
        page.setCurrent(1);
        IPage result=agentOrderService.page(page,wrapper);
        List<AgentOrder> list=result.getRecords();
        for(AgentOrder order : list){
            submitProduce.MakeOrder(order.getOrderCode());
        }
    }

    //提交卡密订单
    @Scheduled(fixedDelay = 60*1000)
    public  void  Card(){
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time=dtf.format(LocalDateTime.now());
        LambdaQueryWrapper<AgentOrder> wrapper=new LambdaQueryWrapper<AgentOrder>();
//        wrapper.and(wrappers->wrappers.eq(AgentOrder::getOrderStatus,OrderEnum.ERROR).or().eq(AgentOrder::getOrderStatus,OrderEnum.UNDERWAY));
        wrapper.and(wrappers->wrappers.eq(AgentOrder::getOrderStatus,OrderEnum.ERROR));
        wrapper.eq(AgentOrder::getProductType, ProductTypeENUM.Card);

        Page page=new Page();
        page.setSize(1000);
        page.setCurrent(1);
        IPage result=agentOrderService.page(page,wrapper);
        List<AgentOrder> list=result.getRecords();;
        // 创建一个固定大小的线程池:
        ExecutorService es = new ThreadPoolExecutor(100, 1000,
                60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
        for(AgentOrder order : list){
            es.submit(new CardTask(order,orderTool));
        }
        es.shutdown();
    }




    //回调
    @Scheduled(fixedDelay = 30*1000)
    public  void  Notice(){
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time=dtf.format(LocalDateTime.now());
        LambdaQueryWrapper<AgentOrder> wrapper=new LambdaQueryWrapper<AgentOrder>();
        wrapper.eq(AgentOrder::getCallback,false);
        wrapper.lt(AgentOrder::getCallbackNum,3);
        wrapper.and(wrappers->wrappers.eq(AgentOrder::getOrderStatus,OrderEnum.SUCCESS).or().eq(AgentOrder::getOrderStatus,OrderEnum.FAILED));

        Page page=new Page();
        page.setSize(6000);
        page.setCurrent(1);
        IPage result=agentOrderService.page(page,wrapper);
        List<AgentOrder> list=result.getRecords();
        for(AgentOrder order : list){
            orderFanoutProduce.OrderNotice(order.getId());
        }
    }

    //退订
    @Scheduled(cron = "0 */1 * * * ?")
    public  void  Refund(){
        LambdaQueryWrapper<OrderRefund> wrapper=new LambdaQueryWrapper<OrderRefund>();
        wrapper.and(wrappers->wrappers.eq(OrderRefund::getOrderStatus,0));


        Page page=new Page();
        page.setSize(1000);
        page.setCurrent(1);
        IPage result=refundService.page(page,wrapper);
        List<OrderRefund> list=result.getRecords();
        // 创建一个固定大小的线程池:
        ExecutorService es = new ThreadPoolExecutor(100, 1000,
                60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
        for(OrderRefund order : list){
            es.submit(new RefundTask(order,orderTool));
        }
        es.shutdown();
    }

//    //退订
//    @Scheduled(cron = "0 */1 * * * ?")
//    public  void  RefundAutoMoTic(){
//        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        String time=dtf.format(LocalDateTime.now());
//        LambdaQueryWrapper<RefundAutomotic> wrapper=new LambdaQueryWrapper<RefundAutomotic>();
//        wrapper.and(wrappers->wrappers.eq(RefundAutomotic::getOrderStatus,0));
//
//
//        Page page=new Page();
//        page.setSize(500);
//        page.setCurrent(1);
//        IPage result=automoticService.page(page,wrapper);
//        List<RefundAutomotic> list=result.getRecords();
//        // 创建一个固定大小的线程池:
//        ExecutorService es = new ThreadPoolExecutor(1, 1000,
//                60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
//        for(RefundAutomotic order : list){
//            es.submit(new AutoMoTicTask(order,orderTool));
//        }
//        es.shutdown();
//    }


//    //退订
//    @Scheduled(cron = "0 */1 * * * ?")
//    public  void  Refund(){
//        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        String time=dtf.format(LocalDateTime.now());
//        LambdaQueryWrapper<RefundOrder> wrapper=new LambdaQueryWrapper<RefundOrder>();
//        wrapper.eq(RefundOrder::getOrderStatus,0);
////        wrapper.and(wrappers->wrappers.eq(RefundOrder::getErrMsg,"操作失败:-1"));
//
//
//        Page page=new Page();
//        page.setSize(500);
//        page.setCurrent(1);
//        IPage result=refundOrderService.page(page,wrapper);
//        List<RefundOrder> list=result.getRecords();
//        // 创建一个固定大小的线程池:
//        ExecutorService es = new ThreadPoolExecutor(1, 1000,
//                60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
//        for(RefundOrder order : list){
//            es.submit(new OrderRefundTask(order,orderTool));
//        }
//        es.shutdown();
//    }

}
